<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\DataBackhaulSetting;
use App\Model\DataPaymentRefund;
use App\Model\RechargeTemplate;
use App\Service\delivery\OrderService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Model\DataSpread;
use App\Common\Response\SystemCode;
use App\Service\playlet\PaymentRefundService;

#[AutoController]
class PaymentRefundController  extends BaseController
{

    #[Inject]
    private PaymentRefundService $paymentRefundService;
    #[Inject]
    private OrderService $orderService;
    /**
     * 退款列表
     */
    public function Index(RequestInterface $request)
    {
        $data = $this->paymentRefundService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);
    }
    /**
     * 申请退款
     */
    public function refundOrder($id)
    {

        if(!empty($id)) {
            $refundInfo = DataPaymentRefund::query()->select('id', 'payment_id', 'type','refund_reason','status')->where('id', $id)->first();
            if (empty($refundInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '退款申请不存在');
            }

            if($refundInfo->status == 3 || $refundInfo->status == 2 ){
                return $this->response(SystemCode::SYSTEM_ERROR, '订单已在处理中或已退款成功,请问重复提交');
            }

            if($refundInfo->type == 1){
                $result  = $this->orderService->refundWxOrder($refundInfo);
            }

            if($refundInfo->type == 2){
                $result  = $this->orderService->refundDyOrder($refundInfo);
            }

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '申请退款成功');
            }else{
                return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

            }
        }

    }




}